/*
 *  stringAddition.h
 *  smsAssistant
 *
 *  Created by sffofn on 11-6-1.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */
#import "sys/utsname.h"

@interface NSString(Addition)
+ (NSString*)getDocumentPath;
+ (NSString *)dataFilePath: (NSString*)fileName;
+ (NSString*)getDeviceType;
+ (NSString*)dataFilePathForPlayVideo:(NSString*)fileName;
+ (NSString*)removePhotoExt:(NSString*)imageName;
+ (NSString*)addPhotoExt:(NSString*)imageName;

- (BOOL)isVideoFormat;
@end

@implementation NSString(Addition)

+(NSString*)getDocumentPath
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 
	return [paths objectAtIndex:0];
}


//得到文件路径
+(NSString*)dataFilePath:(NSString*)fileName
{
	NSString *documentsDiretory = [NSString getDocumentPath];
	return [documentsDiretory stringByAppendingPathComponent:fileName];
}

//专用于视频播放(ios5.0)
+(NSString*)dataFilePathForPlayVideo:(NSString*)fileName
{
    NSString *documentsDiretory = [NSString getDocumentPath];
    NSString *playPath = [NSString stringWithFormat:@"file://localhost/private%@", [documentsDiretory stringByAppendingPathComponent:fileName]];
	return playPath;
}

/*!
 *  get the information of the device and system
 *  "i386"          simulator
 *  "iPod1,1"       iPod Touch
 *  "iPhone1,1"     iPhone
 *  "iPhone1,2"     iPhone 3G
 *  "iPhone2,1"     iPhone 3GS
 *  "iPad1,1"       iPad
 *  "iPhone3,1"     iPhone 4
 *  @return null
 */
+ (NSString*)getDeviceType
{
    //here use sys/utsname.h
    struct utsname systemInfo;
    uname(&systemInfo);
	
	return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    //get the device model and the system version
  //  NSLog(@"%@", [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]);
  //  NSLog(@"%@", [[UIDevice currentDevice] systemVersion]);
}

//完美支持格式
//如果照片名存在格式后缀，则移除
+ (NSString*)removePhotoExt:(NSString*)imageName
{
    NSRange extRange = [imageName rangeOfString:@".png" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        imageName = [imageName substringToIndex:extRange.location];
        return imageName;
    }
    
    extRange = [imageName rangeOfString:@".jpg" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        imageName = [imageName substringToIndex:extRange.location];
        return imageName;
    }
    
    extRange = [imageName rangeOfString:@".mov" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        imageName = [imageName substringToIndex:extRange.location];
        return imageName;
    }
    
    extRange = [imageName rangeOfString:@".m4v" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        imageName = [imageName substringToIndex:extRange.location];
        return imageName;
    }
    
    extRange = [imageName rangeOfString:@".3gp" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        imageName = [imageName substringToIndex:extRange.location];
        return imageName;
    }
    
    extRange = [imageName rangeOfString:@".mp4" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        imageName = [imageName substringToIndex:extRange.location];
        return imageName;
    }
    
    return imageName;
}

//如果照片没有格式后缀，就增加后缀
+ (NSString*)addPhotoExt:(NSString*)imageName
{
    NSRange extRange = [imageName rangeOfString:@".png" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        return imageName;
    }
    
    extRange = [imageName rangeOfString:@".jpg" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        return imageName;
    }
    
    extRange = [imageName rangeOfString:@".mov" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        return [imageName stringByReplacingOccurrencesOfString:@"mov" withString:@"jpg"];
    }
    
    extRange = [imageName rangeOfString:@".m4v" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        return [imageName stringByReplacingOccurrencesOfString:@"m4v" withString:@"jpg"];
    }
    
    extRange = [imageName rangeOfString:@".3gp" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        return [imageName stringByReplacingOccurrencesOfString:@"3gp" withString:@"jpg"];
    }
    
    extRange = [imageName rangeOfString:@".mp4" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        return [imageName stringByReplacingOccurrencesOfString:@"mp4" withString:@"jpg"];
    }
    else 
    {
        imageName = [imageName stringByAppendingString:@".jpg"];
    }
    
    return imageName;

}


- (BOOL)isVideoFormat
{
    NSRange extRange = [self rangeOfString:@".png" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        return NO;
    }
    
    extRange = [self rangeOfString:@".jpg" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        return NO;
    } 
    
    if([self rangeOfString:@".m4v" options:NSBackwardsSearch].location == NSNotFound 
       && [self rangeOfString:@".mov" options:NSBackwardsSearch].location == NSNotFound
       && [self rangeOfString:@".mp4" options:NSBackwardsSearch].location == NSNotFound
       && [self rangeOfString:@".3gp" options:NSBackwardsSearch].location == NSNotFound)
    {
        return NO;
    }
    
    
    return YES;
}









@end
